package com.study.jvm;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class OutOfMemory {
    //java.lang.OutOfMemoryError: GC overhead limit exceeded
    static void gcOverheadLimitExceeded(){
        int count = 0;
        try {
            List<String> strings = new LinkedList<>();
            while (true){
                count++;
                strings.add("aaa" + count);
            }
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
        System.out.println("count = " + count);
    }
    //java.lang.OutOfMemoryError: Java heap space
    static void javaHeapSpace(){
        int i=0;
        try{
            List<String> list = new ArrayList<>();
            String a = "hello";
            while (true){
                list.add(a);
                a = a+ a;
                i++;
            }
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
        System.out.println("i = " + i);

    }
    //-Xmx   -Xmx8m
    public static void main(String[] args) {
        gcOverheadLimitExceeded();
    }
}
